<?php 
declare (strict_types = 1);
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
class BusinessPostComment extends Api
{
    public function add()
    {
        $this->businessCheckLogin();
        $post = param();
        //帖子ID,内容
        if(!isset($post['business_post_id']) || !isset($post['comment'])) return msgerror('参数缺失');
        if(!isset($post['level'])){
            $res = $this->model->where([
                ['business_post_id','=',$post['business_post_id']]
            ])->order('level desc')->find();
            if(!empty($res) && !empty($res->toArray()))
            {
                $res = $res->toArray();
                $level = ($res['level']*1+1);
            }else{
                $level = 1;
            }
        }else{
            $level = $post['level'];
        }
        $data = [
            'business_post_id'=>$post['business_post_id'],
            'content'=>$post['comment'],
            'p_id'=>$post['p_id']??0,
            'level'=>$level,
            'business_id'=>$this->businessinfo['id']
        ];
        $this->model->save($data);
        return datasuccess([],'评论成功');
    }
    //门店帖子-一级评论列表
    public function index()
    {
        $post = param();
        //帖子ID
        if(!isset($post['id'])) return msgerror('参数缺失');
        $limit = $post['limit'] ?? 10;
        $data = $this->model->where(['business_post_id'=>$post['id'],'p_id'=>0])->with(['business','p_business'])->order('id desc')->paginate($limit);
        return datasuccess($data);
    }
    //门店帖子-二级评论列表
    public function two_index()
    {
        $post = param();
        //帖子ID，层数
        if(!isset($post['id']) || !isset($post['level'])) return msgerror('参数缺失');
        $limit = $post['limit'] ?? 5;
        $data = $this->model->where([
            ['business_post_id','=',$post['id']],
            ['level','=',$post['level']],
            ['p_id','<>',0]
        ])->with(['business','p_business'])->order('id desc')->paginate($limit);
        return datasuccess($data);
    }
}